package com.youkeyi.ddy.cloud.application.system.organization.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.organization.SystemOrganizationCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.organization.dto.SystemOrganizationDetailCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.organization.dto.SystemOrganizationListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.organization.dto.SystemOrganizationListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.organization.param.SystemOrganizationCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.organization.param.SystemOrganizationDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.organization.param.SystemOrganizationListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.organization.param.SystemOrganizationUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.position.dto.SystemPositionListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.server.dto.SystemServerListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.PublicCorpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.PublicCorpSortCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpSortListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpSortListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpSortQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.PublicMappingCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.dto.PublicMappingListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.dto.PublicMappingListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.param.PublicMappingQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.PublicOrganizationCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.param.PublicOrganizationCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.param.PublicOrganizationDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.param.PublicOrganizationQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.param.PublicOrganizationUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.PublicPositionCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.dto.PublicPositionListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.param.PublicPositionQueryCloudParam;
import com.youkeyi.ddy.cloud.common.config.TestCorpConfig;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * SystemOrganizationController
 *
 * @Author liuxiawang
 * @Date 2024/2/29 9:28 AM
 * @Location shenzhen.china
 */
@Slf4j
@Service
public class SystemOrganizationController implements SystemOrganizationCloudService {

    @Autowired
    private PublicOrganizationCloudService publicOrganizationCloudService;
    @Autowired
    private PublicPositionCloudService publicPositionCloudService;
    @Autowired
    private PublicCorpCloudService publicCorpCloudService;
    @Autowired
    private PublicMappingCloudService publicMappingCloudService;
    @Autowired
    private PublicCorpSortCloudService publicCorpSortCloudService;

    @Override
    public SystemOrganizationListCloudDto list(SystemOrganizationListCloudParam param) {
        SystemOrganizationListCloudDto dto = new SystemOrganizationListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        Integer isRoot = YoukeyiSystemContext.getIsRoot();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicOrganizationQueryCloudParam organizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
        organizationQueryCloudParam.setOrganizationPrimaryKeys(param.getOrganizationPrimaryKeys());
        if (2 != isRoot) {
            if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
                organizationQueryCloudParam.setCorpKeys(param.getCorpKeys());
            } else {
                organizationQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            }
        }
        organizationQueryCloudParam.setNames(param.getNames());
        organizationQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        organizationQueryCloudParam.setIsShare(param.getIsShare());
        organizationQueryCloudParam.setCodes(param.getCodes());
        organizationQueryCloudParam.setNeedShare(Boolean.TRUE);
        organizationQueryCloudParam.setPageSize(param.getPageSize());
        organizationQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicOrganizationListCloudDto organizationDtos = publicOrganizationCloudService.query(organizationQueryCloudParam);
        if (null != organizationDtos && CollectionUtils.isNotEmpty(organizationDtos.getRows())) {
            // 查询父级数据
            PublicOrganizationQueryCloudParam parentOrganizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
            parentOrganizationQueryCloudParam.setOrganizationPrimaryKeys(organizationDtos.getRows().stream().map(PublicOrganizationListItemCloudDto::getParentKey).collect(Collectors.toList()));
            PublicOrganizationListCloudDto parentOrganizationDtos = publicOrganizationCloudService.query(parentOrganizationQueryCloudParam);
            Map<String, String> parentOrganizationMap = new HashMap<>();
            if (null != parentOrganizationDtos && CollectionUtils.isNotEmpty(parentOrganizationDtos.getRows())) {
                parentOrganizationMap.putAll(parentOrganizationDtos.getRows().stream().collect(Collectors.toMap(PublicOrganizationListItemCloudDto::getOrganizationPrimaryKey, PublicOrganizationListItemCloudDto::getName, (k1, k2) -> k1)));
            }

            List<String> corpKeys = organizationDtos.getRows().stream().map(PublicOrganizationListItemCloudDto::getCorpKey).collect(Collectors.toList());
            Map<String, String> corpMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(corpKeys)) {
                PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
                corpQueryCloudParam.setCorpPrimaryKeys(corpKeys);
                PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);
                if (null != corpDtos && CollectionUtils.isNotEmpty(corpDtos.getRows())) {
                    corpMap.putAll(corpDtos.getRows().stream().collect(Collectors.toMap(PublicCorpListItemCloudDto::getCorpPrimaryKey, PublicCorpListItemCloudDto::getName, (k1, k2) -> k1)));
                }
            }

            List<SystemOrganizationListItemCloudDto> items = new ArrayList<>();
            organizationDtos.getRows().forEach(organization -> {
                SystemOrganizationListItemCloudDto item = new SystemOrganizationListItemCloudDto();
                item.setOrganizationPrimaryKey(organization.getOrganizationPrimaryKey());
                item.setCorpKey(organization.getCorpKey());
                item.setCorpName(corpMap.get(organization.getCorpKey()));
                item.setParentOrganizationPrimaryKey(organization.getParentKey());
                item.setParentOrganizationName(parentOrganizationMap.get(organization.getParentKey()));
                item.setName(organization.getName());
                item.setCode(organization.getCode());
                item.setIsShare(organization.getIsShare());
                item.setLevel(organization.getLevel());
                item.setType(organization.getType());
                item.setSort(organization.getSort());
                item.setResponsibility(organization.getResponsibility());
                item.setLeaderEmployeeKey(organization.getLeaderEmployeeKey());
                item.setLeaderEmployeeName(organization.getLeaderEmployeeName());
                item.setLeaderEmployeePhone(organization.getLeaderEmployeePhone());
                item.setTelephone(organization.getTelephone());
                item.setAddress(organization.getAddress());
                item.setEmail(organization.getEmail());
                item.setDesc(organization.getDesc());
                item.setIsDeleted(organization.getIsDeleted());
                item.setIsDeletedName(organization.getIsDeletedName());
                item.setCreateUserKey(organization.getCreateUserKey());
                item.setCreateTime(organization.getCreateTime());
                item.setCreateUserName(organization.getCreateUserName());
                items.add(item);
            });
            dto.setRows(items);
            if (CollectionUtils.isNotEmpty(dto.getRows())) {
                dto.getRows().sort(Comparator.comparing(SystemOrganizationListItemCloudDto::getSort));
            }
            dto.setTotal(organizationDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemOrganizationListCloudDto mappingList(SystemOrganizationListCloudParam param) {
        SystemOrganizationListCloudDto dto = new SystemOrganizationListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        Integer isRoot = YoukeyiSystemContext.getIsRoot();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicOrganizationQueryCloudParam organizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
        if (null != isRoot && 2 != isRoot) {
            if (CollectionUtils.isEmpty(param.getCorpKeys())) {
                param.getCorpKeys().add(corpKey);
            }
            // 本身的corp的组织也需要查询
            organizationQueryCloudParam.setCorpKeys(param.getCorpKeys());

            // 查询其关联的主体信息
            PublicMappingQueryCloudParam publicMappingQueryCloudParam = new PublicMappingQueryCloudParam();
            publicMappingQueryCloudParam.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_HRCORP_CORP.value());
            publicMappingQueryCloudParam.setKeyType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_SOURCE.value());
            publicMappingQueryCloudParam.setKeys(param.getCorpKeys());
            PublicMappingListCloudDto publicMappingDtos = publicMappingCloudService.query(publicMappingQueryCloudParam);
            if (null != publicMappingDtos && CollectionUtils.isNotEmpty(publicMappingDtos.getRows())) {
                List<String> corpKeys = publicMappingDtos.getRows().stream().map(PublicMappingListItemCloudDto::getTargetKey).collect(Collectors.toList());
                organizationQueryCloudParam.getCorpKeys().addAll(corpKeys);
            }
        }
        organizationQueryCloudParam.setNeedShare(Boolean.TRUE);
        PublicOrganizationListCloudDto organizationDtos = publicOrganizationCloudService.query(organizationQueryCloudParam);
        Map<String, Long> corpSortMap = new HashMap<>();
        if (null != organizationDtos && CollectionUtils.isNotEmpty(organizationDtos.getRows())) {
            // 查询父级数据
            PublicOrganizationQueryCloudParam parentOrganizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
            parentOrganizationQueryCloudParam.setOrganizationPrimaryKeys(organizationDtos.getRows().stream().map(PublicOrganizationListItemCloudDto::getParentKey).collect(Collectors.toList()));
            PublicOrganizationListCloudDto parentOrganizationDtos = publicOrganizationCloudService.query(parentOrganizationQueryCloudParam);
            Map<String, String> parentOrganizationMap = new HashMap<>();
            if (null != parentOrganizationDtos && CollectionUtils.isNotEmpty(parentOrganizationDtos.getRows())) {
                parentOrganizationMap.putAll(parentOrganizationDtos.getRows().stream().collect(Collectors.toMap(PublicOrganizationListItemCloudDto::getOrganizationPrimaryKey, PublicOrganizationListItemCloudDto::getName, (k1, k2) -> k1)));
            }

            List<String> corpKeys = organizationDtos.getRows().stream().map(PublicOrganizationListItemCloudDto::getCorpKey).collect(Collectors.toList());
            Map<String, String> corpMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(corpKeys)) {
                PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
                corpQueryCloudParam.setCorpPrimaryKeys(corpKeys);
                PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);
                if (null != corpDtos && CollectionUtils.isNotEmpty(corpDtos.getRows())) {
                    corpMap.putAll(corpDtos.getRows().stream().collect(Collectors.toMap(PublicCorpListItemCloudDto::getCorpPrimaryKey, PublicCorpListItemCloudDto::getName, (k1, k2) -> k1)));
                }


                PublicCorpSortQueryCloudParam corpSortQueryCloudParam = new PublicCorpSortQueryCloudParam();
                corpSortQueryCloudParam.setCorpPrimaryKeys(param.getCorpKeys());
                PublicCorpSortListCloudDto corpSortDtos = publicCorpSortCloudService.query(corpSortQueryCloudParam);
                if (null != corpSortDtos && CollectionUtils.isNotEmpty(corpSortDtos.getRows())) {
                    corpSortMap.putAll(corpSortDtos.getRows().stream().collect(Collectors.toMap(PublicCorpSortListItemCloudDto::getMappingCorpKey, PublicCorpSortListItemCloudDto::getSort, (k1, k2) -> k1)));
                }
            }

            List<SystemOrganizationListItemCloudDto> items = new ArrayList<>();
            organizationDtos.getRows().forEach(organization -> {
                SystemOrganizationListItemCloudDto item = new SystemOrganizationListItemCloudDto();
                item.setOrganizationPrimaryKey(organization.getOrganizationPrimaryKey());
                item.setCorpKey(organization.getCorpKey());
                item.setCorpName(corpMap.get(organization.getCorpKey()));
                item.setCorpSort(corpSortMap.get(organization.getCorpKey()));
                item.setParentOrganizationPrimaryKey(organization.getParentKey());
                item.setParentOrganizationName(parentOrganizationMap.get(organization.getParentKey()));
                item.setName(organization.getName());
                item.setCode(organization.getCode());
                item.setIsShare(organization.getIsShare());
                item.setLevel(organization.getLevel());
                item.setType(organization.getType());
                item.setSort(organization.getSort());
                item.setResponsibility(organization.getResponsibility());
                item.setLeaderEmployeeKey(organization.getLeaderEmployeeKey());
                item.setLeaderEmployeeName(organization.getLeaderEmployeeName());
                item.setLeaderEmployeePhone(organization.getLeaderEmployeePhone());
                item.setTelephone(organization.getTelephone());
                item.setAddress(organization.getAddress());
                item.setEmail(organization.getEmail());
                item.setDesc(organization.getDesc());
                item.setIsDeleted(organization.getIsDeleted());
                item.setIsDeletedName(organization.getIsDeletedName());
                item.setCreateUserKey(organization.getCreateUserKey());
                item.setCreateTime(organization.getCreateTime());
                item.setCreateUserName(organization.getCreateUserName());
                items.add(item);
            });

            dto.setRows(items);
            dto.setTotal(organizationDtos.getTotal());
        }
        if (CollectionUtils.isNotEmpty(dto.getRows())) {
//            List<SystemOrganizationListItemCloudDto> list = dto.getRows().stream()
//                    .sorted(Comparator.comparing(SystemOrganizationListItemCloudDto::getCorpSort, Comparator.nullsLast(Comparable::compareTo)))
//                    .sorted(Comparator.comparing(SystemOrganizationListItemCloudDto::getSort, Comparator.nullsLast(Comparable::compareTo)))
//                    .collect(Collectors.toList());
            List<SystemOrganizationListItemCloudDto> list = dto.getRows().stream()
                    .sorted(Comparator.comparing(SystemOrganizationListItemCloudDto::getCorpSort, Comparator.nullsLast(Comparator.naturalOrder()))
                            .thenComparing(SystemOrganizationListItemCloudDto::getSort, Comparator.nullsLast(Comparator.naturalOrder())))
                    .collect(Collectors.toList());
            dto.getRows().clear();
            dto.setRows(list);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemOrganizationListCloudDto all() {
        SystemOrganizationListCloudDto dto = new SystemOrganizationListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }

        PublicOrganizationQueryCloudParam organizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
        // 非测试主体就查询的时候剔除测试主体
        if (!TestCorpConfig.TestCorpKey.contains(corpKey)) {
            organizationQueryCloudParam.setNotInCorpKeys(Arrays.asList(TestCorpConfig.TestCorpKey.split("\\|")));
        }
        organizationQueryCloudParam.setNeedShare(Boolean.TRUE);
        PublicOrganizationListCloudDto organizationDtos = publicOrganizationCloudService.query(organizationQueryCloudParam);
        if (null != organizationDtos && CollectionUtils.isNotEmpty(organizationDtos.getRows())) {
            List<String> corpKeys = organizationDtos.getRows().stream().map(PublicOrganizationListItemCloudDto::getCorpKey).collect(Collectors.toList());
            Map<String, String> corpMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(corpKeys)) {
                PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
                corpQueryCloudParam.setCorpPrimaryKeys(corpKeys);
                PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);
                if (null != corpDtos && CollectionUtils.isNotEmpty(corpDtos.getRows())) {
                    corpMap.putAll(corpDtos.getRows().stream().collect(Collectors.toMap(PublicCorpListItemCloudDto::getCorpPrimaryKey, PublicCorpListItemCloudDto::getName, (k1, k2) -> k1)));
                }
            }

            List<SystemOrganizationListItemCloudDto> items = new ArrayList<>();
            organizationDtos.getRows().forEach(organization -> {
                String corpName = corpMap.get(organization.getCorpKey());
                if (StringUtils.isNotBlank(corpName)) {
                    SystemOrganizationListItemCloudDto item = new SystemOrganizationListItemCloudDto();
                    item.setOrganizationPrimaryKey(organization.getOrganizationPrimaryKey());
                    item.setCorpKey(organization.getCorpKey());
                    item.setCorpName(corpName);
                    item.setName(organization.getName());
                    item.setSort(item.getSort());
                    item.setIsShare(organization.getIsShare());
                    item.setCode(organization.getCode());
                    item.setLevel(organization.getLevel());
                    item.setType(organization.getType());
                    items.add(item);
                }
            });
            dto.setRows(items);
            // dto.setTotal(organizationDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemOrganizationDetailCloudDto detail(String organizationPrimaryKey) {
        SystemOrganizationDetailCloudDto dto = new SystemOrganizationDetailCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (StringUtils.isBlank(organizationPrimaryKey)) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicOrganizationQueryCloudParam organizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
        organizationQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        organizationQueryCloudParam.setOrganizationPrimaryKeys(Collections.singletonList(organizationPrimaryKey));
        organizationQueryCloudParam.setNeedServer(Boolean.TRUE);
        PublicOrganizationListCloudDto organizations = publicOrganizationCloudService.query(organizationQueryCloudParam);
        if (null != organizations && CollectionUtils.isNotEmpty(organizations.getRows())) {
            PublicOrganizationListItemCloudDto organization = organizations.getRows().get(0);
            dto.setOrganizationPrimaryKey(organization.getOrganizationPrimaryKey());
            dto.setName(organization.getName());
            dto.setOrganizationCode(organization.getCode());
            dto.setLevel(organization.getLevel());
            dto.setType(organization.getType());
            dto.setParentKey(organization.getParentKey());
            dto.setIsShare(organization.getIsShare());
            dto.setIsDeleted(organization.getIsDeleted());
            dto.setIsDeletedName(organization.getIsDeletedName());
            dto.setCreateUserKey(organization.getCreateUserKey());
            dto.setCreateUserName(organization.getCreateUserName());
            dto.setCreateTime(organization.getCreateTime());
            dto.setUpdateUserKey(organization.getUpdateUserKey());
            dto.setUpdateUserName(organization.getUpdateUserName());
            dto.setUpdateTime(organization.getUpdateTime());

            // 服务信息
            if (CollectionUtils.isNotEmpty(organization.getServers())) {
                organization.getServers().forEach(server -> {
                    SystemServerListItemCloudDto serverItem = new SystemServerListItemCloudDto();
                    serverItem.setServerPrimaryKey(server.getServerPrimaryKey());
                    serverItem.setCode(server.getCode());
                    serverItem.setName(server.getName());
                    dto.getServers().add(serverItem);
                });
            }
            // 岗位信息
            PublicPositionQueryCloudParam positionQueryCloudParam = new PublicPositionQueryCloudParam();
            positionQueryCloudParam.setOrganizationKeys(Collections.singletonList(organizationPrimaryKey));
            positionQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            PublicPositionListCloudDto positionDtos = publicPositionCloudService.query(positionQueryCloudParam);
            if (null != positionDtos && CollectionUtils.isNotEmpty(positionDtos.getRows())) {
                positionDtos.getRows().forEach(position -> {
                    SystemPositionListItemCloudDto positionItem = new SystemPositionListItemCloudDto();
                    positionItem.setPositionPrimaryKey(position.getPositionPrimaryKey());
                    positionItem.setName(position.getName());
                    positionItem.setCode(position.getCode());
                    positionItem.setDesc(position.getDesc());
                    dto.getPositions().add(positionItem);
                });
            }
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(SystemOrganizationCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getCode())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicOrganizationCreateCloudParam organizationCreateCloudParam = new PublicOrganizationCreateCloudParam();
        organizationCreateCloudParam.setCorpKey(StringUtils.defaultIfEmpty(param.getCorpKey(), corpKey));
        organizationCreateCloudParam.setName(param.getName());
        organizationCreateCloudParam.setCode(param.getCode());
        organizationCreateCloudParam.setType(param.getType());
        organizationCreateCloudParam.setSort(param.getSort());
        organizationCreateCloudParam.setIsShare(param.getIsShare());
        organizationCreateCloudParam.setResponsibility(param.getResponsibility());
        organizationCreateCloudParam.setLeaderEmployeeKey(param.getLeaderEmployeeKey());
        organizationCreateCloudParam.setLeaderEmployeeName(param.getLeaderEmployeeName());
        organizationCreateCloudParam.setLeaderEmployeePhone(param.getLeaderEmployeePhone());
        organizationCreateCloudParam.setTelephone(param.getTelephone());
        organizationCreateCloudParam.setAddress(param.getAddress());
        organizationCreateCloudParam.setEmail(param.getEmail());
        organizationCreateCloudParam.setDesc(param.getDesc());
        organizationCreateCloudParam.setParentKey(param.getParentKey());
        organizationCreateCloudParam.setServerPrimaryKeys(param.getServerPrimaryKeys());
        organizationCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        organizationCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicOrganizationCloudService.create(organizationCreateCloudParam);
    }

    @Override
    public ResponseContent update(SystemOrganizationUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getOrganizationPrimaryKey())
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getCode())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicOrganizationUpdateCloudParam organizationUpdateCloudParam = new PublicOrganizationUpdateCloudParam();
        organizationUpdateCloudParam.setOrganizationPrimaryKey(param.getOrganizationPrimaryKey());
        organizationUpdateCloudParam.setName(param.getName());
        organizationUpdateCloudParam.setCode(param.getCode());
        organizationUpdateCloudParam.setIsShare(param.getIsShare());
        organizationUpdateCloudParam.setType(param.getType());
        organizationUpdateCloudParam.setSort(param.getSort());
        organizationUpdateCloudParam.setResponsibility(param.getResponsibility());
        organizationUpdateCloudParam.setLeaderEmployeeKey(param.getLeaderEmployeeKey());
        organizationUpdateCloudParam.setLeaderEmployeeName(param.getLeaderEmployeeName());
        organizationUpdateCloudParam.setLeaderEmployeePhone(param.getLeaderEmployeePhone());
        organizationUpdateCloudParam.setTelephone(param.getTelephone());
        organizationUpdateCloudParam.setAddress(param.getAddress());
        organizationUpdateCloudParam.setEmail(param.getEmail());
        organizationUpdateCloudParam.setDesc(param.getDesc());
        organizationUpdateCloudParam.setParentKey(param.getParentKey());
        organizationUpdateCloudParam.setServerPrimaryKeys(param.getServerPrimaryKeys());
        organizationUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        organizationUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicOrganizationCloudService.update(organizationUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(SystemOrganizationDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getOrganizationPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicOrganizationDeleteCloudParam organizationDeleteCloudParam = new PublicOrganizationDeleteCloudParam();
        organizationDeleteCloudParam.setOrganizationPrimaryKeys(param.getOrganizationPrimaryKeys());
        organizationDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        organizationDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicOrganizationCloudService.delete(organizationDeleteCloudParam);
    }
}
